package org.corespring.restaurant.web.controller;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.corespring.restaurant.model.Noticia;
import org.corespring.restaurant.service.NoticiaManager;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.validation.BindingResult;
import javax.servlet.http.HttpServletRequest;
import org.corespring.restaurant.model.Usuario;
import org.corespring.restaurant.service.UsuarioManager;

@Controller
public class NoticiaController {

    NoticiaManager noticiaManager;
    UsuarioManager usuarioManager;

    @Autowired
    public NoticiaController(NoticiaManager oNoticiaManager, UsuarioManager oUsuarioManager){
        this.noticiaManager=oNoticiaManager;
        this.usuarioManager = oUsuarioManager;
    }

    @RequestMapping("/adm_noticias.htm")
    public void adm_noticias(Model model, @ModelAttribute("Noticia")Noticia noticia ){
        List<Noticia> noticias= noticiaManager.obtenerNoticias();
        model.addAttribute("noticias",noticias);
        model.addAttribute("noticia",noticia);
    }

    @RequestMapping(value = "/adm_registroNoticia.htm", method = RequestMethod.GET)
    public
    @ModelAttribute("noticia")
    Noticia obtenerNoticia(
            @RequestParam(value = "idnoticia", required = false) String idNoticia, HttpServletRequest request) {
        if(idNoticia ==null){
             return new Noticia();
        }
        if (!(idNoticia.isEmpty() || idNoticia.equals(""))) {
            Noticia noticia = noticiaManager.obtenerNoticia(new Integer(idNoticia));
            if (noticia == null) {
                return new Noticia();
            }
            return noticia;
        }
        return new Noticia();
    }

    @RequestMapping(value = "/adm_registroNoticia.htm", method = RequestMethod.POST)
    public String guardarNoticia( @RequestParam(value = "cboEstado", required = false) char estado,
            @ModelAttribute("noticia") Noticia noticia,
            BindingResult result, SessionStatus status) {
        Date fecha = new Date();
        String hora = fecha.getHours()+":"+fecha.getMinutes()+":"+fecha.getSeconds();
        Usuario usu = usuarioManager.obtenerUsuarioById("admin");

        noticia.setEstado(estado);
        noticia.setFreg(fecha);
        noticia.setHreg(hora);
        noticia.setUsuario(usu);
        
        noticiaManager.grabarNoticia(noticia);
        return "redirect:adm_noticias.htm";
    }

    @RequestMapping("/eliminarNoticia.htm")
    public String eliminarNoticia(
            @RequestParam(value = "idnoticia", required = false) String idNoticia) {
        noticiaManager.eliminarNoticia(new Integer(idNoticia));
        return "redirect:adm_noticias.htm";
    }

     @RequestMapping("/noticias.htm")
    public void noticias(Model model, @ModelAttribute("Noticia") Noticia noticia) {
		List<Noticia> noticias = noticiaManager.obtenerNoticias();
                model.addAttribute("noticias", noticias);
                model.addAttribute("noticia", noticia);
    }

    @RequestMapping(value = "/noticiasdet.htm", method = RequestMethod.GET)
    public
    @ModelAttribute("noticia")
    Noticia noticiasdet(
            @RequestParam(value = "idnoticia", required = false) String idNoticia, HttpServletRequest request) {
        if(idNoticia ==null){
             return new Noticia();
        }
        if (!(idNoticia.isEmpty() || idNoticia.equals(""))) {
            Noticia noticia = noticiaManager.obtenerNoticia(new Integer(idNoticia));
            if (noticia == null) {
                return new Noticia();
            }
            return noticia;
        }
        return new Noticia();
    }

}
